package edu.pku.sei.modeler.uml.commands;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.Kernel.Comment;
import edu.pku.sei.modeler.model.uml.Kernel.UMLElement;

/**
 * 
 * modified by fengchao
 * 
 */
public class UMLCommentLinkDeleteCommand extends ConnectionModelDeleteCommand {

	public UMLCommentLinkDeleteCommand(MetaModelElement connectionModel,
			MetaModelElement sourceModel, MetaModelElement targetModel) {
		super(connectionModel, sourceModel, targetModel);
	}

	@Override
	public void execute() {
		Comment comment = (Comment) this.sourceModel;
		((UMLElement) targetModel).getOwnedComment().remove(comment);
		comment.getAnnotatedElement().remove(targetModel);

		Dependency commentLink = (Dependency) connectionModel;
		commentLink.getClientDependency().remove(this.targetModel);
		comment.getCommentLink().remove(commentLink);
	}

	@Override
	public void undo() {
		Comment comment = (Comment) this.sourceModel;
		((UMLElement) targetModel).getOwnedComment().add(comment);
		comment.getAnnotatedElement().add(targetModel);

		Dependency commentLink = (Dependency) connectionModel;
		commentLink.setContainer(comment);
		commentLink.getClientDependency().add(this.targetModel);
		commentLink.setModel(comment.getModel());
		comment.getCommentLink().add(commentLink);
	}

	@Override
	public boolean canExecute() {
		return true;
	}
}
